<?php

namespace App\Console\Commands;

use App\Library\Logic\MigrationsLogic;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class MigrateCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:migrate {table}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     * @throws Exception;
     */
    public function handle(): void
    {
        if (prod()) return;
        $table = $this->argument('table');
        foreach (explode(',', $table) as $v) $this->item(humpToLine(lcfirst($v)));
    }

    /**
     * item
     * @param string $table
     */
    private function item(string $table): void
    {
        if ($table === 'migrations') return;
        $file = database_path('migrations/sql/' . $table . '.sql');
        if (!is_file($file)) return;
        $file = database_path('migrations/' . $this->migration($table) . '.php');
        if (!is_file($file)) copy(database_path('base_class.txt'), $file);
        if (!in_array($table, $this->tables())) return;
        $where = ['migration' => $this->migration($table)];
        $migrations = MigrationsLogic::findByWhere($where);
        if (!is_null($migrations)) $migrations->delete();
        if (!prod()) Artisan::call('migrate --force');
    }

    private function migration(string $table): string
    {
        return '2024_01_01_000000_create_' . $table . '_table';
    }

    private function tables(): array
    {
        return [
            'activity_zone',
            'admin_menu',
            'admin_operation_log',
            'admin_permissions',
            'admin_role_menu',
            'admin_role_permissions',
            'admin_role_users',
            'admin_roles',
            'admin_user_permissions',
            'admin_users',
            'audit',
            'bank',
            'box',
            'box_spec',
            'buy_will',
            'demo',
            'env',
            'invite',
            'invite_certification',
            'failed_jobs',
            'message',
            'message_list',
            'password_reset_tokens',
            'personal_access_tokens',
            'users',
            'shop_express_delivery',
            'shop_images',
            'order_split',
            'shop_order',
            'shop_order_ext',
            'shop_order_goods',
            'shop_order_return',
            'shop_product',
            'shop_product_album',
            'shop_product_deposit_price',
            'shop_product_specs',
            'shop_user_price_flowing_log',
            'shop_user_price_flowing_log_new',
            'shop_user_price_waiting',
            'payment',
            'payment_method',
            'payment_related',
            'refund',
            'notify',
            'auction',
            'auction_bail',
            'auction_bid',
            'auction_certification',
            'auction_follow',
            'auction_order',
            'after_sale',
            'cart',
            'amount',
            'sales_record',
            'withdraw',
            'adjustment',
            'index_module',
            'slide',
            'media_relation',
            'ticket',
            'user_account',
            'sms',
            'shop_feedback',
            'real_name_auth',
            'user_dm',
            'shop_user_collect',
            'source_material',
            'source_material_submit_record',
            'system_config',
            'shop_handbook',
            'open_id',
            'timing_picture',
            'sql_error',
            'import_product',
            'operate_log',
            'logistics',
            'coupon',
            'coupon_user_related',
            'pet',
            'shop_bank_card',
            'buy_will',
            'follow',
            'message_push_record',
            'kefu',
            'reception',
            'shop_customer_im',
            'shop_user_message',
            'message_list',
            'message',
            'follow',
            'message_push_record',
            'zb_bail',
            'area',
            'address',
            'import_plug',
            'area',
            'address',
            'import_plug',
            'zb_bail',
            'shop_product_ext',
            'coupon',
            'coupon_user_related',
            'help',
            'help_record',
        ];
    }
}
